package com.miplus.ccrm.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.query.MPJQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.admin.controller.vo.NamesResVo;
import com.miplus.ccrm.admin.controller.vo.PermEditReqVo;
import com.miplus.ccrm.admin.controller.vo.PermissionResVo;
import com.miplus.ccrm.admin.pojo.SysPermission;
import com.miplus.ccrm.admin.service.SysPermissionService;
import com.miplus.ccrm.admin.mapper.SysPermissionMapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author pc
* @description 针对表【sys_permission(系统权限表)】的数据库操作Service实现
* @createDate 2024-03-06 15:53:30
*/
@Service
public class SysPermissionServiceImpl extends MPJBaseServiceImpl<SysPermissionMapper, SysPermission>
    implements SysPermissionService{

    private void addChildNode(PermissionResVo resVo, List<PermissionResVo> all) {
        List<PermissionResVo> tempList = all.stream()
                .filter(d -> resVo.getId().equals(d.getPid()))
                .collect(Collectors.toList());
        resVo.setChildren(tempList);
        tempList.forEach(d -> {
            //添加子节点
            addChildNode(d,all);
        });
    }

    private List<PermissionResVo> listTree(List<PermissionResVo> all)
    {
        List<PermissionResVo> tree = new ArrayList<>();
        //获取到最外层的组信息
        List<PermissionResVo> plist = all.stream().filter(d -> JPubUtils.IsEmpty(d.getPid())).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(plist)){
            for(PermissionResVo permission : plist) {
                tree.add(permission);
                addChildNode(permission, all);
            }
        }
        return tree;
    }

    @Override
    public List<PermissionResVo> permissionTree()
    {
        List<PermissionResVo> all=getBaseMapper().selectJoinList(
                PermissionResVo.class,
                new MPJQueryWrapper<SysPermission>().selectAll(SysPermission.class)
        );
        return listTree(all);
    }

    @Override
    public List<PermissionResVo> activePermissionTree()
    {
        List<PermissionResVo> all=getBaseMapper().selectJoinList(PermissionResVo.class,
                new MPJQueryWrapper<SysPermission>().eq("status","1").selectAll(SysPermission.class)
        );
        return listTree(all);
    }

    @Override
    public List<NamesResVo> activePermissionList(String parentid)
    {
        return selectJoinList(NamesResVo.class,
                new MPJLambdaWrapper<SysPermission>()
                        .selectAs(SysPermission::getName,NamesResVo::getLabel)
                        .selectAs(SysPermission::getId,NamesResVo::getValue)
                        .selectAll(SysPermission.class)
                        .eq("status","1")
                        .eq("parentid",parentid)
                        .orderByAsc(SysPermission::getId)
        );
    }

    @Override
    public CmdResVo updatePermission(PermEditReqVo paras)
    {
        SysPermission perm=new SysPermission();
        BeanUtils.copyProperties(paras,perm);
        int ret=getBaseMapper().updateById(perm);

        return new CmdResVo(ret);
    }

    @Override
    public CmdResVo insertPermission(PermEditReqVo paras)
    {
        SysPermission perm=new SysPermission();
        BeanUtils.copyProperties(paras,perm);
        int ret=getBaseMapper().insert(perm);

        return new CmdResVo(ret);
    }

    @Override
    public CmdResVo deletePermission(String permid)
    {
        int ret=getBaseMapper().deleteById(permid);
        return new CmdResVo(ret);
    }

    @Override
    public CmdResVo updatePermissionStatus(String permid,String status)
    {
        SysPermission perm=new SysPermission();
        perm.setId(permid);
        perm.setStatus(status);
        int ret=getBaseMapper().updateById(perm);
        return new CmdResVo(ret);
    }
}




